#include "EventHelper.h"
#include "Namespace.h"

#include <QApplication>
#include <QMimeData>

bool Ev::keyIsDown(QEvent *event) {
    QList<QEvent::Type> types{QEvent::KeyPress, QEvent::ShortcutOverride};
    return types.contains(event->type());
}

bool Ev::isUnusableKey(Qt::Key key) {
    QList<Qt::Key> keys{Qt::Key_CapsLock, Qt::Key_NumLock, Qt::Key_ScrollLock, Qt::Key_Meta};
    return keys.contains(key);
}

bool Ev::isSpecialKey(Qt::Key key) {
    QList<Qt::Key> keys{Qt::Key_Tab, Qt::Key_Escape, Qt::Key_Return, Qt::Key_Enter};
    return keys.contains(key);
}

bool Ev::isModifierKey(Qt::Key key) {
    QList<Qt::Key> keys{Qt::Key_Control, Qt::Key_Shift, Qt::Key_Alt};
    return keys.contains(key);
}

bool Ev::isSpecialShortcut(QKeySequence shortcut) {
    QList<QKeySequence> shortcuts{QKeySequence("Tab")};
    return shortcuts.contains(shortcut);
}

QMimeData *Ev::createDragMime(const QObject *obj, int id) {
    QMimeData *mime = new QMimeData();
    mime->setData(My::MIME_DRAG_IDENTITY, QString::number(qApp->applicationPid()).toUtf8());
    mime->setData(My::MIME_DRAG_TYPE, QByteArray(obj->metaObject()->className()));
    if (id >= 0) {
        mime->setData(My::MIME_DRAG_ID, QString::number(id).toUtf8());
    }
    return mime;
}

bool Ev::isValidDragMime(const QObject *obj, const QMimeData *mime) {
    if (mime->hasFormat(My::MIME_DRAG_IDENTITY)) {
        QByteArray identity = mime->data(My::MIME_DRAG_IDENTITY);
        if (identity == QString::number(qApp->applicationPid()).toUtf8()) {
            if (mime->hasFormat(My::MIME_DRAG_TYPE)) {
                QByteArray type = mime->data(My::MIME_DRAG_TYPE);
                if (type == QByteArray(obj->metaObject()->className())) {
                    return true;
                }
            }
        }
    }
    return false;
}
